//
//  DownloadModel.m
//  VPPublicUntilitisForPod
//
//  Created by verne on 2017/3/20.
//  Copyright © 2017年 vernepung. All rights reserved.
//

#import "DownloadModel.h"

@implementation DownloadModel
- (NSString *)mainId
{
    id param = [self getParameterOfUrl:self.fileUrl];
    if ([param isKindOfClass:[NSDictionary class]]&&[[param allKeys] containsObject:@"security-token"]) {
        /*AliCloud*/
        NSString *localhostUrl =  param[@"localhost"];
        return [NSString stringWithFormat:@"%@", localhostUrl];
    } else{
        return [NSString stringWithFormat:@"%@", self.fileUrl];
    }
}

- (id)getParameterOfUrl:(NSString *)urlVlaueString
{
    NSRange range = [urlVlaueString rangeOfString:@"?"];
    if (range.length) {
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
        NSString *parameterUrl = [urlVlaueString substringFromIndex:range.location+1];
        NSArray *parameterArray = [parameterUrl componentsSeparatedByString:@"&"];
        for (NSString *parameter in parameterArray) {
            NSArray *parameterBoby = [parameter componentsSeparatedByString:@"="];
            if (parameterBoby.count == 2) {
                [dictionary setObject:parameterBoby[1] forKey:parameterBoby[0]];
            } else{
                return @"Illegal character";
            }
        }
        NSString *localhostString = [urlVlaueString substringToIndex:range.location];
        [dictionary setObject:localhostString forKey:@"localhost"];
        return dictionary;
    } else{
        return @"No parameters";
    }
}

@end
